﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.NhaDat;
using SouthernLand.Common.Untilities;

namespace SouthernLand.WebMaster.RaoVat
{
    public partial class Default : Page
    {
        private eTrangthai Trangthai;

        private string SortBy
        {
            set { ViewState[UniqueID + "_SortID"] = value; }
            get
            {
                if (ViewState[UniqueID + "_SortID"] != null)
                {
                    return (string) ViewState[UniqueID + "_SortID"];
                }
                return "";
            }
        }

        private string SortDirection
        {
            set { ViewState[UniqueID + "_SortDirection"] = value; }
            get
            {
                if (ViewState[UniqueID + "_SortDirection"] != null)
                {
                    return (string) ViewState[UniqueID + "_SortDirection"];
                }
                return "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Load_TrangThai();

            Pager.IndexChanged += PageIndex_Changed;
            if (!Page.IsPostBack)
                DataBind_Raovat();
        }

        private void Load_TrangThai()
        {
            string strTrangthai = Globals.GetStringFromQueryString("Trangthai");
            if (strTrangthai == eTrangthai.TinVip.ToString().Trim())
            {
                Title1.Text = "Tin Vip <img src=" + Globals.ImagesUrl() + "vip.gif>";
                Trangthai = eTrangthai.TinVip;
            }
            else if (strTrangthai == eTrangthai.HetHan.ToString().Trim())
            {
                Title1.Text = "Tin Hết Hạn";
                Trangthai = eTrangthai.HetHan;
            }
            else
            {
                Title1.Text = "Tin Hiệu Lực";
                Trangthai = eTrangthai.HieuLuc;
            }
        }

        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind_Raovat();
        }

        protected string GetThongKe(int RaovatID)
        {
            string strText = "";
            if (RaovatID > 0)
            {
                Raovat obj = Raovat.GetRaovatByID(RaovatID);
                strText = strText + "Loại tin: " + Loaitin.GetTenLoaiTinByID(obj.Loaitin) + "<br>";
                strText = strText + "Loại BĐS: " + Common.BOL.NhaDat.Categories.GetCateNameByCateID(obj.CateID) + "<br>";
                strText = strText + "Người đăng tin: " + Common.BOL.User.User.GetUserNameByUserID(obj.UserID) + "<br>";
                strText = strText + "Số lần xem: " + obj.TotalViews + "<br>";
            }
            return strText;
        }

        public void DataBind_Raovat()
        {
            Pager.PageSize = 15;
            string keyword = txtKeyword.Text.Trim();
            var objRaovat = new Raovat();
            objRaovat.Loaitin = -1;
            objRaovat.CateID = ConvertUtility.ToInt32(dlLoaiBDS.SelectedItem.Value);
            objRaovat.Tinhthanh = -1;
            objRaovat.Quanhuyen = -1;
            objRaovat.HuongNha = -1;
            objRaovat.TinhtrangPL = -1;

            List<Raovat> lSearch = Raovat.Search(Trangthai, keyword, objRaovat, "", -1, "", SortBy + " " + SortDirection,
                                                 Pager.PageIndex, Pager.PageSize);
            GridViewItem.DataSource = lSearch;
            GridViewItem.DataBind();

            int TotalRecords = 0;
            foreach (Raovat f in lSearch)
            {
                TotalRecords = f.TotalRecords;
                break;
            }

            Pager.TotalRecords = CurrentPage.TotalRecords = TotalRecords;
            CurrentPage.TotalPages = Pager.CalculateTotalPages();
            CurrentPage.PageIndex = Pager.PageIndex;

            // kiem tra xem co can hien thi phan trang ko
            DisplayPager.Visible = Pager.TotalRecords > 1;
        }

        protected void btSort_Click(object sender, EventArgs e)
        {
            var btSort = (LinkButton) sender;
            SortDirection = SortDirection.ToLower() == "asc" ? "desc" : "asc";
            SortBy = btSort.ID;
            Pager.PageIndex = 1;
            // load lai du lieu
            DataBind_Raovat();
        }
        
        /// <summary>
        // chi su dung khi khach hang xem tin het han 
        // tin het han va thuoc loai vip se hien thi ra Image nay
        /// </summary>
        /// <param name="RaovatID"></param>
        /// <returns></returns>
        protected string GetImageVip(int RaovatID)
        {
            string strTrangthai = Globals.GetStringFromQueryString("Trangthai");
            if (strTrangthai == eTrangthai.HetHan.ToString().Trim())
            {
                Raovat obj = Raovat.GetRaovatByID(RaovatID);
                if (obj.DisplayOrder == 100)
                    return "<img src=" + Globals.ImagesUrl() + "vip.gif>";
            }
            return "";
        }

        protected string GetImageSort(string Sort)
        {
            if (Sort.ToLower() == SortBy.ToLower())
            {
                if (SortDirection.ToLower() == "asc")
                    return " <img src=" + Globals.ImagesUrlWebMaster() + "asc.gif align=absmiddle border=0 />";
                else if (SortDirection.ToLower() == "desc")
                    return " <img src=" + Globals.ImagesUrlWebMaster() + "desc.gif align=absmiddle border=0 />";
            }
            else
                return " ";
            return "";
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridViewItem.Rows)
            {
                bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int RaovatID = Convert.ToInt32(((CheckBox) row.FindControl("chkSelect")).ToolTip.Trim());
                    Raovat.Delete(RaovatID);
                }
            }
            DataBind_Raovat();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            DataBind_Raovat();
        }
    }
}